package com.ruihu.admin.module.system.org.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruihu.admin.common.domain.PageResultDTO;
import com.ruihu.admin.common.domain.ResponseDTO;
import com.ruihu.admin.module.system.org.dao.OrgDao;
import com.ruihu.admin.module.system.org.domain.dto.OrgAddDTO;
import com.ruihu.admin.module.system.org.domain.dto.OrgUpdateDTO;
import com.ruihu.admin.module.system.org.domain.dto.OrgQueryDTO;
import com.ruihu.admin.module.system.org.domain.entity.OrgEntity;
import com.ruihu.admin.module.system.org.domain.vo.OrgVO;
import com.ruihu.admin.module.system.org.domain.vo.OrgExcelVO;
import com.ruihu.admin.util.BeanCopierUtil;
import com.ruihu.admin.util.PageUtil;
import com.ruihu.admin.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * [  ]
 *
 * @author Tony
 * @version 1.0
 * @company 瑞虎（南京）信息技术有限公司
 * @copyright (c)  瑞虎（南京）信息技术有限公司Inc. All rights reserved.
 * @date 2022-08-30 23:50:33
 * @since JDK1.8
 */
@Service
public class OrgService {

    @Autowired
    private OrgDao orgDao;

    /**
     * 根据id查询
     */
    public OrgEntity getById(Long id){
        return  orgDao.selectById(id);
    }

    /**
     * 分页查询
     * @author Tony
     * @date 2022-08-30 23:50:33
     */
    public ResponseDTO<PageResultDTO<OrgVO>> queryByPage(OrgQueryDTO queryDTO) {
        Page page = PageUtil.convert2QueryPage(queryDTO);
        List<OrgVO> voList = orgDao.queryByPage(page, queryDTO);
        page.setRecords(voList);
        PageResultDTO<OrgVO> pageResultDTO = PageUtil.convert2PageResult(page);
        return ResponseDTO.succData(pageResultDTO);
    }

    /**
     * 获取所有部门
     *
     * @return
     */
    public ResponseDTO<List<OrgVO>> listAll() {
        List<OrgVO> departmentVOList = orgDao.listAll();
        return ResponseDTO.succData(departmentVOList);
    }

    /**
     * 添加
     * @author Tony
     * @date 2022-08-30 23:50:33
     */
    public ResponseDTO<Long> add(OrgAddDTO addDTO) {
        OrgEntity entity = BeanCopierUtil.copy(addDTO, OrgEntity.class);
        entity.setIsDelete(0);
        entity.setSort(0);
        orgDao.insert(entity);
        entity.setSort(entity.getId().intValue());
        orgDao.updateById(entity);
        return ResponseDTO.succData(entity.getId());
    }

    /**
     * 编辑
     * @author Tony
     * @date 2022-08-30 23:50:33
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(OrgUpdateDTO updateDTO) {
        OrgEntity entity = BeanCopierUtil.copy(updateDTO, OrgEntity.class);
        orgDao.updateById(entity);
        return ResponseDTO.succ();
    }

    /**
     * 删除
     * @author Tony
     * @date 2022-08-30 23:50:33
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> deleteByIds(List<Long> idList) {
        orgDao.deleteByIdList(idList);
        return ResponseDTO.succ();
    }

    /**
     * 查询全部导出对象
     * @author Tony
     * @date 2022-08-30 23:50:33
     */
    public List<OrgExcelVO> queryAllExportData(OrgQueryDTO queryDTO) {
        return orgDao.queryAllExportData( queryDTO);
    }

    /**
     * 批量查询导出对象
     * @author Tony
     * @date 2022-08-30 23:50:33
     */
    public List<OrgExcelVO> queryBatchExportData(List<Long> idList) {
        return orgDao.queryBatchExportData(idList);
    }
}
